/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.hazard.setting.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.hopes.bizz.common.core.constant.CommonConstant;
import com.hopes.bizz.common.core.validate.ValidatorUtils;
import com.hopes.bizz.hazard.common.constant.enums.HazardSysDictEnum;
import com.hopes.bizz.hazard.setting.dto.HazBasicFactorConfigExcelDTO;
import com.hopes.bizz.hazard.setting.dto.HazBasicRtuConfigExcelDTO;
import com.hopes.bizz.hazard.setting.entity.HazBasicRtuConfigEntity;
import com.hopes.bizz.hazard.setting.mapper.HazBasicRtuConfigMapper;
import com.hopes.bizz.hazard.setting.service.HazBasicRtuConfigService;
import com.hopes.bizz.hazard.setting.vo.HazBasicRtuConfigExcelVO;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.admin.api.resolver.enums.AdminDictEnum;
import com.hopes.boss.admin.api.resolver.enums.SysDictEnum;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 数采仪管理
 *
 * @author daijie
 * @date 2023-11-01 10:15:39
 */
@Service
public class HazBasicRtuConfigServiceImpl extends ServiceImpl<HazBasicRtuConfigMapper, HazBasicRtuConfigEntity> implements HazBasicRtuConfigService {

    @Override
    public boolean save(HazBasicRtuConfigEntity entity) {
        entity.setBrokenNetFlag(CommonConstant.boolean_false_string);
        return SqlHelper.retBool(getBaseMapper().insert(entity));
    }

    @Override
    public ValidateImportBO<HazBasicRtuConfigExcelDTO> validateImport(List<HazBasicRtuConfigExcelDTO> excelDTOList) {
        ValidateImportBO<HazBasicRtuConfigExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<HazBasicRtuConfigExcelDTO> saveList = new ArrayList<>();
        int successNum = 0;
        int failedNum = 0;


        for (HazBasicRtuConfigExcelDTO hazBasicRtuConfigExcelDTO : excelDTOList) {
            final String validateRes = ValidatorUtils.validateEntity(hazBasicRtuConfigExcelDTO);
            if (StrUtil.isEmpty(validateRes)) {
                successNum++;
                saveList.add(hazBasicRtuConfigExcelDTO);
            } else {
                failedNum++;
                errorMessageList.add(new ErrorMessage(hazBasicRtuConfigExcelDTO.getLineNum(), new HashSet<>() {
                    private static final long serialVersionUID = 6173748630741688040L;
                    {
                        add(validateRes);
                    }
                }));
            }
        }
        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public HazBasicRtuConfigEntity importDtoToEntity(HazBasicRtuConfigExcelDTO excelDto) {
        final HazBasicRtuConfigEntity entity = new HazBasicRtuConfigEntity();
        BeanUtils.copyProperties(excelDto, entity);
        if (StrUtil.isNotEmpty(excelDto.getUseFlagStr())) {
            entity.setUseFlag(DictResolver.getItemValueByLabel(SysDictEnum.yes_no_type, excelDto.getUseFlagStr()));
        }
        if (StrUtil.isNotEmpty(excelDto.getOrgIdStr())) {
            entity.setOrgId(DictResolver.getItemValueByLabel(AdminDictEnum.sys_company, excelDto.getOrgIdStr(), Long::valueOf));
        }
        if (StrUtil.isNotEmpty(excelDto.getProtocolTypeStr())) {
            entity.setProtocolType(DictResolver.getItemValueByLabel(HazardSysDictEnum.park_protocol_type, excelDto.getProtocolTypeStr()));
        }
        entity.setBrokenNetFlag(CommonConstant.boolean_false_string);
        return entity;
    }

    @Override
    public HazBasicRtuConfigExcelVO exportEntityToVo(HazBasicRtuConfigEntity entity) {
        final HazBasicRtuConfigExcelVO excelVO = new HazBasicRtuConfigExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        excelVO.setOrgIdStr(entity.getOrgIdStr());
        excelVO.setUseFlagStr(entity.getUseFlagStr());
        excelVO.setProtocolTypeStr(entity.getProtocolTypeStr());
        return excelVO;
    }
}
